package com.tjetc.view.manage;

import com.tjetc.domain.Admin;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.AdminService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class AdminView {
        private  Scanner scanner = new Scanner(System.in);
        private AdminService adminService = ClientProxy.getProxyInstance(AdminService.class);
    public void start() {
        while(true){
            try {
                System.out.println("1.查询所有管理员");
                System.out.println("2.添加管理员");
                System.out.println("3.修改管理员信息");
                System.out.println("4.删除管理员");
                System.out.println("5.按照姓名查询管理员");
                System.out.println("0.返回上一级");
                int i= scanner.nextInt();
                switch (i){
                    case 1:
                        list();
                        break;
                    case 2:
                        add();
                        break;
                    case 3:
                        update();
                        break;
                    case 4:
                        delete();
                        break;
                    case 5:
                        getAdminByName();
                        break;
                    case 0:
                        return;
                }
            }catch (Exception e){
                System.out.println("输入错误,请重新输入");
                scanner.nextLine();
            }
        }
    }
    private void getAdminByName() {
        System.out.println("请输入管理员姓名");
        String username = scanner.next();
        Admin admin = adminService.getAdminsByName(username);
        if (admin==null){
            System.out.println("管理员不存在");
        }else{
            System.out.println(admin);
        }
    }

    private void delete() {
        System.out.println("请输入管理员id");
        int id = scanner.nextInt();
        Result result = adminService.delete(id);
        System.out.println(result.getMsg());
        System.out.println( result.getMsg());
    }

    private void update() {
        System.out.println("请输入管理员id");
        int id = scanner.nextInt();
        System.out.println("请输入请输入管理员的姓名");
        String username = scanner.next();
        System.out.println("请输入请输入管理员的密码");
        String password = scanner.next();
        Admin admin = new Admin(id,username,password);
        Result result = adminService.update(admin);
        System.out.println(result.getMsg());
    }

    private void add() {
        System.out.println("请输入请输入管理员的姓名");
        String username = scanner.next();
        System.out.println("请输入请输入管理员的密码");
        String password = scanner.next();
        Admin admin = new Admin(username,password);
        Result result = adminService.register(admin);
        System.out.println(result.getMsg());
    }

    private void list() {
        List<Admin> list= adminService.getAdminsList();
        for (Admin admin : list) {
            System.out.println(admin);
        }
    }
}
